package com.ctsi.security.manager.impl;

import com.ctsi.organise.common.api.OrganiseFactoryBean;
import com.ctsi.organise.common.api.OrganiseServiceProxy;
import com.ctsi.organise.common.model.Organise;
import com.ctsi.security.OrganiseIn;
import com.ctsi.security.RolePartyType;
import com.ctsi.security.domain.SRoleUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Component
public class OrganiseInImpl implements OrganiseIn {
	
	private static final Logger log = LoggerFactory.getLogger(OrganiseInImpl.class);
	/**
	 * 取组织或用户组下的所有登陆用户
	 * 
	 * @param partyId
	 * @return
	 */
	public List<SRoleUser> getOrganOrUserGroupToLoginUser(String partyId, String type) {
		List<SRoleUser> listReturn = new ArrayList<SRoleUser>();
		//取组织的登陆用户
		List<Map> listLoginUser =null;
		if(type.equals(SRoleUser.typeOrgan)){
			 listLoginUser = findAllUserDes(partyId, RolePartyType.ORGANISE);
		}else{
			 listLoginUser = findAllUserDes(partyId,RolePartyType.GROUP);
		}
			for (Map loginUser : listLoginUser) {
				String loginId = (String) loginUser.get("LOGINID");
				loginId = loginId == null ? "" : loginId;
				String loginName = (String) loginUser.get("FIRSTNAME")
						+ (String) loginUser.get("LASTNAME");
				loginName = loginName == null ? "" : loginName;
				String partyId0 = (String) loginUser.get("PARTYID");
				partyId0 = partyId0 == null ? "" : partyId0;
				String parentId = (String) loginUser.get("ORGANISEID");
				parentId = parentId == null ? "" : parentId;
				SRoleUser sroleUser=new SRoleUser();
				sroleUser.setLoginId(loginId);
				sroleUser.setLoginName(loginName);
				sroleUser.setPartyId(partyId0);
				sroleUser.setParentId(parentId);
				listReturn.add(sroleUser);
			}	
		return listReturn;
	}

	/**
	 * 取组织或用户组下的所有子组织
	 * 
	 * @param partyId
	 * @return
	 */
	public List<SRoleUser> getOrganOrUserGroupToOrgan(String partyId,String type) {
		List<SRoleUser> listReturn = new ArrayList<>();
		List<Organise> listOrgan=null;
		if(type.equals(SRoleUser.typeOrgan)){//取组织下的组织
			listOrgan = findAllOrganDes(partyId,RolePartyType.ORGANISE);
		}else{
			listOrgan = findAllOrganDes(partyId,RolePartyType.GROUP);
		}
			for (Organise organ : listOrgan) {
				SRoleUser sroleUser=new SRoleUser();
				sroleUser.setLoginName(organ.getPartyName());
				sroleUser.setPartyId(organ.getPartyId());
				sroleUser.setParentId(organ.getParentId());
				listReturn.add(sroleUser);
			}	
		return listReturn;
	}
	private OrganiseServiceProxy organService;
	/**
	 * 一个组织是否包含另一个组织
	 * 
	 * @param parentId
	 * @param sonId
	 * @return
	 */
	public boolean isContainOrgan(String parentId, String sonId) {
		return getOrganService().isContain(parentId, sonId);
	}

	public OrganiseServiceProxy getOrganService() {
		if(organService==null){
			organService = OrganiseFactoryBean.getInstance().getOrganiseProxy();
		}
		return organService;
	}

	public void setOrganService(OrganiseServiceProxy organService) {
		this.organService = organService;
	}
	
	public List<Map> findAllUserDes(String partyId, String partyType) {
		if(partyType.equals(RolePartyType.GROUP)){
			//return OrganiseFactoryBean.getInstance().getUserGroupProxy().findAllUserLoginDes(partyId);
			return Collections.emptyList();
		}else if(partyType.equals(RolePartyType.ORGANISE)){
			return getOrganService().findAllUserDes(partyId);
		}else{
			log.warn("not support partyType {}",partyType);
		}
		return new ArrayList<Map>(0);
	}
	public List<Organise> findAllOrganDes(String partyId, String partyType) {
		if(partyType.equals(RolePartyType.GROUP)){
		  //return OrganiseFactoryBean.getInstance().getUserGroupProxy().findAllOrganDes(partyId);
            return Collections.emptyList();
		}else if(partyType.equals(RolePartyType.ORGANISE)){
		  return (List<Organise>) getOrganService().findAllOrganDes(partyId);
		}else{
			log.warn("not support partyType {}",partyType);
		}
		return new ArrayList<Organise>(0);
	}

}
